package com.vcar.LDiceClient.chat
{
	import com.vcar.LDiceClient.config.Config;
	import com.vcar.LDiceClient.config.GlobalEventName;
	import com.vcar.LDiceClient.config.MediatorName;
	import com.vcar.LDiceClient.config.ProxyNames;
	import com.vcar.LDiceClient.core.ModWindowManager;
	import com.vcar.LDiceClient.core.PopUpManager;
	import com.vcar.LDiceClient.dataproxy.RequestMessage;
	import com.vcar.LDiceClient.dataproxy.chat.chatProxy;
	import com.vcar.LDiceClient.dataproxy.user.UserProxy;
	
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.ui.Keyboard;
	
	import mx.controls.Button;
	import mx.utils.UIDUtil;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class ChatFrameMediator extends Mediator
	{
		public static const WORLD:int = 2;
		public static const AREA:int = 3;
		public static const PASSPHRASE:int = 4;
		private static const channeldp:Array=[{label:"世界",data:1},{label:"区域",data:2},{label:"私聊",data:3}];
		private var emPopUp:EmPopUp;
		public function ChatFrameMediator(viewComponent:Object=null)
		{
			super(MediatorName.ChatMediator, viewComponent);
			ChatFrame.sendBtn.addEventListener(MouseEvent.CLICK,sendMsg);
			ChatFrame.ExpreBtn.addEventListener(MouseEvent.CLICK,showExpreList);
			ChatFrame.tfin.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
			ChatFrame.channelSel.dataProvider = channeldp;
			ChatFrame.channelSel.selectedIndex=0;
		}
		
		public function get ChatFrame():FChatFrame
		{
			return viewComponent as FChatFrame;
		}
		
		override public function listNotificationInterests() : Array
		{
			return [];
		}
		
		override public function handleNotification(notification:INotification) : void
		{

		}
		

		/**
		 * 发送消息
		 * @author	Rices
		 */
		private function sendMsg(e:MouseEvent=null):void
		{
			if( ChatFrame.tfin.text==""){
				return;
			}
			var sendText:String = ChatFrame.tfin.text.replace(/#(\d+)/g, "[smile=$1]");
			var tChatProxy:chatProxy = facade.retrieveProxy(ProxyNames.ChatProxy) as chatProxy;
			var tRequestMsg:RequestMessage = new RequestMessage();
			tRequestMsg.type = 3;
			tRequestMsg.subtype = ChatFrame.channelSel.selectedItem.data;
			tRequestMsg.params.msg = sendText;
			tChatProxy.sendMsg(tRequestMsg);
			ChatFrame.tfin.text='';
		}
		
		/**
		 * 键盘点击发送消息
		 * @author	Rices
		 */		
		private function keyDownHandler(e:KeyboardEvent):void
		{
			if(e.keyCode==Keyboard.ENTER && ChatFrame.tfin.text!=""){
				sendMsg();
				e.stopImmediatePropagation();
				e.stopPropagation();
			}
		}
		
		/**
		 *点击弹出显示表情输入框
		 * @author	Rices
		 */		
		private function showExpreList(e:MouseEvent):void
		{
			if(emPopUp!=null) {
				PopUpManager.removePopUp(emPopUp);	
				emPopUp=null;
				return;
			}			
			emPopUp=new EmPopUp();
			emPopUp.addEventListener(ChatExObjectEvent.CLICK,onEmPopupClick);
			var btn:Button = e.currentTarget as Button;
			
			var pos:Point=btn.localToGlobal(new Point(btn.width+2,-emPopUp.height));	
			if(pos.x+emPopUp.width>965) pos.x=965-emPopUp.width-4;			
			emPopUp.x=pos.x;
			emPopUp.y=pos.y;
			ModWindowManager.getInstance().addComponentPopUp(emPopUp);
		}
		
		/**
		 * 隐藏表情输入框
		 */
		private function onEmPopupClick(e:ChatExObjectEvent):void
		{
			insertEm(ChatSmileObject(e.exObject).smileId);				
			PopUpManager.removePopUp(emPopUp);	
			emPopUp=null;
		}
		
		private function insertEm(id:int):void
		{
			ChatFrame.tfin.text+="#"+id+" ";
		}
	}
}